<?php

namespace app\home\controller;

use app\home\def\Helper;
use app\home\def\Status;
use app\home\model\Resource as indexModel;
use think\Db;
use think\Url;

class Index extends Base
{
    private $indexModel;
    private $userModel;


    public function __construct()
    {
        parent::__construct();
        $this->indexModel = new indexModel();
        $this->userModel  = new \app\home\model\User();
        Url::root('/index.php');
    }

    /**
     * 主页
     * @return mixed
     */
    public function index()
    {

        $cats = $this->indexModel->table('rs_resource_category')->limit(7)->select();
        $this->assign('lu',$this->lastUser());
        $this->assign('cats', $cats);
        return $this->fetch();
    }

    /**
     * @return array
     */
    public function lastResApi()
    {
        $last = $this->indexModel->getResource('', 10);
        return $this->err->info(0, Helper::parseResData($last));
    }

    /**
     * 详情
     * @param $guid
     * @return \think\response\View
     */
    public function detail($guid = '')
    {
        Db::table('rs_resource')->where('PK_ResourceGuid', $guid)->setInc('PageView');
        $resInfo = $this->indexModel->with(['user', 'category', 'rescomment', 'rescomment.user'])->find($guid);
        $this->assign('i', $resInfo);
        return view();
    }

    /**
     * 顶的最多
     */
    public function maxtops()
    {
        return $this->err->info(0, Helper::parseResData($this->indexModel->getResourceWithOrderAndLimit('Tops desc', 6)));
    }

    /**
     * 浏览的最多
     */
    public function maxPageviews()
    {
        return $this->err->info(0, Helper::parseResData($this->indexModel->getResourceWithOrderAndLimit('PageView desc', 10)));

    }

    /**
     * 最贵
     */
    public function maxPrice()
    {
        return $this->err->info(0, Helper::parseResData($this->indexModel->getResourceWithOrderAndLimit('Price desc', 10)));

    }

    /**
     * 浏览的最多
     */
    public function maxDownloads()
    {
        return $this->err->info(0, Helper::parseResData($this->indexModel->getResourceWithOrderAndLimit('Downloads desc', 10)));
    }

    /**
     * search
     */
    public function search($q = '')
    {
        $this->assign('q', $q);
        return view();
    }

    /**
     * searchAPI
     */
    public function searchApi($q = '')
    {
        $res = $this->indexModel->with(['user', 'category', 'rescomment'])->order('CreateTime desc')->where('Title', 'like', "%$q%")->whereOr('Desc', 'like', "%$q%")->where(Status::status('pass'))->order("CreateTime desc")->limit(100)->select();
        if ($res) {
            return $this->err->info(0, $this->indexModel->getAjaxResData($res));
        } else {
            return $this->err->info(3);
        }
    }

    /**
     * 板块
     * @param $q
     * @return \think\response\View
     */
    public function plate($type = '')
    {
        $this->assign('type', $type);
        return view();
    }


    public function plateApi($type)
    {
        switch ($type) {
            case "newShare": {
                $res = $this->indexModel->with(['user', 'category', 'rescomment'])->order('CreateTime desc')->limit(100)->where(Status::$pass)->select();
                if ($res) {
                    return $this->err->info(0, $this->indexModel->getAjaxResData($res));
                } else {
                    return $this->err->info(3);
                }
            }
                break;
            case "question": {
                $res = $this->indexModel->with(['user', 'category', 'rescomment'])->where('type', '问题')->where(Status::$pass)->order('CreateTime desc')->limit(100)->select();
                if ($res) {
                    return $this->err->info(0, $this->indexModel->getAjaxResData($res));
                } else {
                    return $this->err->info(3);
                }
            }
                break;
            default:
                return $this->err->info(3);
                break;
        }
    }



    //模块扩展地方


    /**
     * 页面分类api
     */
    public function resPageApi($page = 1, $limit = 10, $where = [], $type = '')
    {

        switch ($type) {
            case "newShare": {
                $searche = $this->indexModel->with(['user', 'category', 'rescomment'])->page($page, $limit)->order("CreateTime desc")->where($where)->select();
                return $this->err->info(0, Helper::parseResData($searche));
            }
                break;
            case "question": {
                $searche = $this->indexModel->with(['user', 'category', 'rescomment'])->page($page, $limit)->order("CreateTime desc")->where($where)->where('type', '问题')->select();
                return $this->err->info(0, Helper::parseResData($searche));
            }
                break;
            default: {
                return $this->err->info(4);
            }
                break;
        }

    }


    /**
     * 页面分页api的分页数量
     */
    public function resPageApiCount($type = '', $where = [])
    {
        switch ($type) {
            case "newShare": {
                $searche = $this->indexModel->with(['user', 'category', 'rescomment'])->where($where)->where(Status::$pass)->count();
                return $this->err->info(0, $searche);
            }
                break;
            case "question": {
                $searche = $this->indexModel->with(['user', 'category', 'rescomment'])->where($where)->where(Status::$pass)->where('type', '问题')->count();
                return $this->err->info(0, $searche);
            }
                break;
            default: {
                return $this->err->info(4);
            }
                break;
        }
    }


    public function lastUser()
    {
        return $this->userModel->with('recomment')->order('CreateTime desc')->limit(20)->select();
    }

}
